package com.intramirror.product.controller;

import com.intramirror.core.common.response.Response;
import com.intramirror.core.common.response.StatusCode;
import static com.intramirror.product.constant.AggregateConstant.TYPE_MOVE_TO_NEW_SPU;
import com.intramirror.product.entity.Product;
import com.intramirror.product.entity.Spu;
import com.intramirror.product.entity.SpuAggregateLog;
import com.intramirror.product.exception.BusinessException;
import com.intramirror.product.service.IAggregateService;
import com.intramirror.product.service.IProductService;
import com.intramirror.product.service.ISpuService;
import com.intramirror.product.utils.AggregateRuleManagement;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

/**
 * Created on 2018/1/31.
 * @author 123
 */
@RestController
public class AggregateController {
    private static final Logger LOGGER = LoggerFactory.getLogger(AggregateController.class);

    @Autowired
    IAggregateService aggregateService;

    @Autowired
    ISpuService spuService;

    @Autowired
    IProductService productService;

    @PutMapping(value = "/product/aggregate/spu")
    public Response aggregateProductToNewSpu(@SessionAttribute(value = "user") Map<String, Object> user, @RequestParam(value = "productId") Long productId,
            @RequestParam(value = "designerId") String designerId, @RequestParam(value = "colorCode") String colorCode) {

        Spu srcSpu = null;
        Product p = productService.selectByPrimaryKey(productId);
        if (p != null) {
            if (p.getSpuId() != null) {
                srcSpu = spuService.getSpuById(p.getSpuId());
            }
        }

        Spu spu = aggregateService.aggregateProductToNewSpu(productId, designerId, colorCode);
        if (spu == null) {
            return Response.status(StatusCode.FAILURE).data("DesignerId and color code is exists");
        }

        //重新处理图片
        spu.setCoverImg(spuService.dealCoverImg(spu));
        spu.setDescImg(spuService.dealDescImg(spu));
        spuService.updateSpu(spu);

        //记录日志
        SpuAggregateLog sal = new SpuAggregateLog();

        if (srcSpu != null) {
            sal.setSrcSpuId(srcSpu.getSpuId());
            sal.setSrcDesignerId(srcSpu.getDesignerId());
            sal.setSrcColorCode(srcSpu.getColorCode());
        }

        sal.setTargetSpuId(spu.getSpuId());
        sal.setTargetDesignerId(designerId);
        sal.setTargetColorCode(colorCode);

        sal.setType(TYPE_MOVE_TO_NEW_SPU);
        sal.setProductId(productId);
        sal.setUserId((Long) user.get("userId"));
        spuService.saveAggregateLog(sal);

        return Response.status(StatusCode.SUCCESS).data(spu);
    }

    @GetMapping(value = "/aggregate/spu/start")
    public Response startAggregate() {
        LOGGER.info("Set aggregate flag as start.");
        AggregateRuleManagement.getInstance().setAggregateFlag(true);
        return Response.status(StatusCode.SUCCESS).data("Set start is success.");
    }

    @GetMapping(value = "/aggregate/spu/stop")
    public Response stopAggregate() {
        LOGGER.info("Set aggregate flag as stop.");
        AggregateRuleManagement.getInstance().setAggregateFlag(false);
        return Response.status(StatusCode.SUCCESS).data("Set stop is success.");
    }

    @GetMapping(value = "/aggregate/spu")
    public Response aggregateOneProduct(@RequestParam(value = "productId") Long productId) {
        LOGGER.info("Start to aggregate product: {}.", productId);
        Product product = aggregateService.selectByPrimaryKey(productId);
        try {
            aggregateService.aggregateProduct(product);
        } catch (BusinessException e) {
            LOGGER.error("Could not aggregate product {}.", product.getProductId());
        }
        return Response.status(StatusCode.SUCCESS).data("finish");
    }
}
